#!/bin/bash
# sharefile.sh <file_name>
# cp file from sharedir(/workdir/yqchen/tmp)


######## custom function ########
share_sofie () {
	# sharefile.sh on sofie.pku.edu.cn
	if [ -f ./"$1" ];then
		if [ -f "/workdir/yqchen/tmp/$1" ];then
			echo "sharedir has $1! replace it or not?(y or n)"
			read choice
			case $choice in
				y|yes|Y|Yes )
					rm -f "/workdir/yqchen/tmp/$1"
					cp $1 /workdir/yqchen/tmp
					;;
				n|no|N|No )
					exit 1
					;;
			esac
		else
			echo "copy $1 to sharedir"
			cp $1 /workdir/yqchen/tmp
		fi
	else
		echo "copy $1 from sharedir"
		cp /workdir/yqchen/tmp/"$1" .
	fi
}

share_SidNote () {
	ShareDir="$DESKTOP"
	if [ -f ./$1 ];then
		if [ -f "$ShareDir/$1" ];then
			echo "ShareDir has $1! Replace it or not?(y or n)"
			read choice
			case $choice in
				y|yes|Y|Yes )
					rm -f "$ShareDir/$1"
					cp $1 "$ShareDir"
					;;
				n|no|N|No )
					return 1
					;;
			esac
		else
			echo "copy $1 to sharedir"
			cp $1 "$ShareDir"
		fi
	else
		echo "copy $1 from sharedir"
		cp "$ShareDir/$1"
	fi
}

share_siddor () {
	SSHKEY=/home/sid/.ssh/id_ed25519
	if [ -f ./"$1" ];then
		echo "copy $1 to server"
		scp -i $SSHKEY "$1" yqchen@sofie.pku.edu.cn:/workdir/yqchen/tmp


	else
		echo "copy $1 from server"
		scp -i $SSHKEY yqchen@sofie.pku.edu.cn:/workdir/yqchen/tmp/"$1" .
	fi
}


######## Main ########
case $HOSTNAME in
	sofie|nodeda|node9 )
		share_sofie "$1"
		;;
	SidNote )
		share_SidNote "$1"
		;;
	siddor )
		share_siddor "$1"
		;;
esac
